//
//  PlatformStyleViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/24.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "PlatformStyleViewController.h"
#import "HomePageCollectionCell.h"
#import "ProductDetailsViewController.h"
#import "CategoryViewController.h"

@interface PlatformStyleViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *jsonArray;

@end

static NSString *const homeIdentifier = @"HomePageCollectionCell";

@implementation PlatformStyleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleName;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomePageCollectionCell" bundle:nil] forCellWithReuseIdentifier:homeIdentifier];
    [self requestData];
}

- (void)leftButtonItem {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [backButton addTarget:self action:@selector(backToPrevious) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton_back = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"backIcon"]];
    imageView.frame=CGRectMake(0, 4, 14, 20);
    [backButton addSubview:imageView];
    [self.navigationItem setLeftBarButtonItem:barButton_back];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.ifCategory) {
        [self leftButtonItem];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIUtil colorWithHexString:appFontColor]}];
    } else {
        [self titleView:self.titleName];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"品类" style:UIBarButtonItemStyleBordered target:self action:@selector(categoryButton)];
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        [self.navigationController.navigationBar setTintColor:[UIUtil colorWithHexString:appWhiteColor]];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UINavigationbar"] forBarMetrics:UIBarMetricsDefault];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData {
    NSString *string = @"";
    if ([self.titleName isEqualToString:@"我的收藏"]) {
        string = @"6";
    } else if ([self.titleName isEqualToString:@"平台现款"]) {
        string = @"1";
    } else {
        string = @"1";
    }
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":string, @"productType":self.type == nil?@"":self.type,@"productStyle":self.style == nil?@"":self.style, @"designerId":@""};
    [RequestData requestProductParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.jsonArray = jsonArray;
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark - Action Methods
- (void)categoryButton {
    CategoryViewController *view = [[CategoryViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
}

- (void) backToPrevious {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.jsonArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[HomePageCollectionCell alloc] init];
    }
    cell.model = [_jsonArray objectAtIndex:indexPath.row];
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductDetailsViewController *view = [[ProductDetailsViewController alloc] init];
    view.productId = ((ProductModel *)[_jsonArray objectAtIndex:indexPath.row]).productId;
    view.productStatus = ((ProductModel *)[_jsonArray objectAtIndex:indexPath.row]).productStatus;
    view.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDTH-6)/2, (SCREEN_WIDTH - 6)/2 * 188 / 157 +70);
}

@end
